# By gxt_kt

# After change ~/.config/tmux/tmux.conf, It's best to kill tmux and restart it to make the change take effect.
# $tmux kill-server && tmux || tmux

# If you cannot start tmux, use following cmd to kill tmux completely and then restart tmux
# $pkill -9 tmux

# If first use this config. It's necessary to download tmp manually.
# if you use my whole dotfiles with tmux, the tpm is exist and no need to download again.
# https://github.com/tmux-plugins/tpm
# How to use tmp to manager plugins:
# 1. prefix+I install plugins
# 2. prefix+U update plugins
# 3. prefix+Alt+u remove plugins that not used

set -g mouse on

# add for support image show in tmux
set -gq allow-passthrough on
set -g visual-activity off

# Notice that whatever config the color:
# Please make sure three points:
# 1.You can use Italic font
# 2.Background color and highlight show find. Compare to use alacritty without tmux.
# 3.You can use home and end. If you cannot but the aboves are normal, you can remap home and end below.
set -g default-terminal "screen-256color"
set-option -g default-terminal "tmux-256color" 
set -ga terminal-overrides ",xterm-256color:Tc"
set -as terminal-overrides ',xterm*:sitm=\E[3m'

# solve bug that macos cannot change miniconda python3
# https://stackoverflow.com/questions/57660263/tmux-recognised-conda-env-but-still-use-the-default-python
if-shell 'test "$(uname)" = "Darwin"' 'set -g default-command "/bin/zsh"'

# Becase set the 256color. Cause home and end cannot use. Please remap them.
# bind-key -n Home send Escape "OH"
# bind-key -n End send Escape "OF"

# Share clipboard with system
set-option -g set-clipboard on

#🔽🔽🔽
# TMUX color config
# tmux color https://i.stack.imgur.com/e63et.png
set -g mode-style "fg=default,bg=default,reverse"
set -g status-bg color147
set -g status-fg white
set -g pane-border-style fg=default      
set -ga pane-border-style bg=color147
set -g pane-active-border-style fg=black
set -ga pane-active-border-style bg=yellow
setw -g window-status-style fg=black
setw -g window-status-current-style fg=color160
setw -g window-status-activity-style fg=white
setw -g window-status-bell-style fg=white
set -g message-style bg=black,fg=orange
#🔼🔼🔼

SCRIPTS_PATH="$HOME/.config/tmux/scripts"

# reload the $HOME/.config/tmux/tmux.conf file with prefix+Ctrl-a
bind-key C-a source-file ~/.config/tmux/tmux.conf \; display-message "configuration reloaded"

# tmux detach current session and back to ordinary bash
bind-key d detach-client

# set the prefix
# unbind C-b
# set -g prefix C-a

# set-option -g allow-rename off # disable tmux auto fresh window name

# set pane title
# bind-key t command-prompt "select-pane -T '%%'"
bind-key t command-prompt -p "(rename-pane)" -I "#T" "select-pane -T '%%'"
set -g pane-border-status top
set -g pane-border-format "#P #T"

# select pane
bind -n C-k select-pane -U
bind -n C-j select-pane -D
bind -n C-h select-pane -L
bind -n C-l select-pane -R

# select pane
bind-key k select-pane -U
bind-key j select-pane -D
bind-key h select-pane -L
bind-key l select-pane -R

# copy-mode-vi
set-window-option -g mode-keys vi 
# use cmd{ :list-keys -T copy-mode-vi } to show the full table of commands in copy mode.
# ref: https://superuser.com/questions/395158/tmux-copy-mode-select-text-block
bind-key y copy-mode    # enter copy mode
# bind p paste-buffer     # paste content
bind-key -T copy-mode-vi "v" send -X begin-selection
bind-key -T copy-mode-vi "V" send-keys -X select-line
bind-key -T copy-mode-vi "C-v" send -X rectangle-toggle
bind-key -T copy-mode-vi "y" send -X copy-selection-and-cancel
bind-key -T copy-mode-vi "Y" send-keys -X copy-pipe-end-of-line-and-cancel


# vertical split (prefix -)
# unbind '"'
bind '-' split-window -v -c "#{pane_current_path}"
# horizontal split (prefix |)
# unbind %
bind '\' split-window -h -c "#{pane_current_path}"
bind 'BSpace' split-window -h -c "#{pane_current_path}"

# move pane in one window
bind-key ',' swap-pane -U
bind-key '.' swap-pane -D
bind-key '{' swap-pane -U # tmux default keymap
bind-key '}' swap-pane -D # tmux default keymap
# move pane between two window (Ref: https://unix.stackexchange.com/questions/14300/moving-tmux-pane-to-window)
bind-key M command-prompt -p "join pane from:"  "join-pane -s ':%%'"
bind-key m command-prompt -p "send pane to:" "run-shell '$SCRIPTS_PATH/smart-join-pane.sh \"%%\"'"

# rename window
bind-key 'r' command-prompt -p "Rename window:" "rename-window '%%'"

# set Alt+num goto window num
bind -n M-1 select-window -t 1
bind -n M-2 select-window -t 2
bind -n M-3 select-window -t 3
bind -n M-4 select-window -t 4
bind -n M-5 select-window -t 5
bind -n M-6 select-window -t 6
bind -n M-7 select-window -t 7
bind -n M-8 select-window -t 8
bind -n M-9 select-window -t 9

# kill pane
unbind q 
bind-key q kill-pane

# kill window
unbind Q 
bind-key Q confirm kill-window

# size windows based on clients looking at that window
setw -g aggressive-resize on

# resize pane
bind-key -n C-Left  "resize-pane -L 5"
bind-key -n C-Right "resize-pane -R 5"
bind-key -n C-Up    "resize-pane -U 5"
bind-key -n C-Down  "resize-pane -D 5"

# use keymap with vim
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' { if -F '#{pane_at_left}' '' 'select-pane -L' }
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' { if -F '#{pane_at_bottom}' '' 'select-pane -D' }
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' { if -F '#{pane_at_top}' '' 'select-pane -U' }
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' { if -F '#{pane_at_right}' '' 'select-pane -R' }
bind-key -T copy-mode-vi 'C-h' if -F '#{pane_at_left}' '' 'select-pane -L'
bind-key -T copy-mode-vi 'C-j' if -F '#{pane_at_bottom}' '' 'select-pane -D'
bind-key -T copy-mode-vi 'C-k' if -F '#{pane_at_top}' '' 'select-pane -U'
bind-key -T copy-mode-vi 'C-l' if -F '#{pane_at_right}' '' 'select-pane -R'

# start window numbers at 1 to match keyboard order with tmux order
set -g base-index 1
# start pane numbers at 1 to match keyboard order with tmux order
setw -g pane-base-index 1
# renumber windows sequentially after closing any of them
set -g renumber-windows on
# monitor windows for activity
setw -g monitor-activity on
# don't wait for escape sequences
set -sg escape-time 0
# display pane numbers for longer
set -g display-panes-time 2000
# increase scrollback lines
set -g history-limit 65536

# prefix+@ breaks current pane into separate window
# join a pane/window from the current session into the current window
bind @ command-prompt -p "create pane from:" "join-pane -s ':%%'"

bind-key "?" display-popup -w 100% -h 100% -E 'comm -23 <(tmux list-keys | sort) <(tmux -L test -f /dev/null list-keys | sort) | fzf -e -i --height="100%" --preview="echo {}" --preview-window="wrap" --prompt="tmux hotkeys: " --info=inline --layout=reverse --tiebreak=index --header "prefix=yes-prefix root=no-prefix" > /dev/null'

#🔽🔽🔽
# set status bar
set -g status-interval 2 # 状态栏刷新时间
set -g status-justify left # 状态栏列表左对齐 left/centre/right
setw -g monitor-activity on # 非当前窗口有内容更新时在状态栏通知
set -g status-left-length 50
set -g status-right-length 50
set -g status-left "#[fg=black][#{session_name}] #[align=centre]#[fg=black]#H "  

# set-option -g status-right '#(echo "123")'
# $(echo $(( `cat /sys/class/thermal/thermal_zone0/temp` / 1000)))
# #(echo $(( `cat /sys/class/thermal/thermal_zone0/temp` / 1000))) 
set -g status-right '#[bg=yellow]#[fg=black] #{battery_status_bg}#{battery_icon}#{battery_percentage} \
#{cpu_bg_color}#[fg=black] #{cpu_icon}#{cpu_percentage} \
#[bg=pink] 🕒%a %h-%d %H:%M '
#🔼🔼🔼


#🔽🔽🔽
# set tpm plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'github_username/plugin_name#branch'
# set -g @plugin 'git@github.com:user/plugin'
# set -g @plugin 'git@bitbucket.com:user/plugin'
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'tmux-plugins/tmux-battery'

# set -g @plugin 'jaclu/tmux-menus'

set -g @plugin 'tmux-plugins/tmux-open' 
set -g @plugin 'trevarj/tmux-open-nvim'
set -g @open 'C-o'
set -g @open-editor 'o'
set -g @open-O 'https://www.google.com/search?q='


# prefix+C-s save prefix+C-r reload
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @resurrect-capture-pane-contents 'on'
# 默认恢复 @resurrect-default-processes="vi vim nvim emacs man less more tail top htop irssi weechat mutt"
set -g @resurrect-processes "ssh btop ranger yazi lazygit gitui fzf"
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'off' # last saved environment is automatically restored when tmux is started.
set -g @continuum-boot 'off'    # automatic tmux start

set -g @plugin 'laktak/extrakto'
set -g @extrakto_copy_key "tab"      # use tab to copy to clipboard
set -g @extrakto_insert_key "enter"  # use enter to insert selection
set -g @extrakto_fzf_unset_default_opts "false"  # keep our custom FZF_DEFAULT_OPTS

set -g @plugin 'Morantron/tmux-fingers'
set -g @fingers-key s

# set -g @plugin 'tmux-plugins/tmux-yank'
# set -g @plugin 'CrispyConductor/tmux-copy-toolkit'
# set -g @copytk-copy-command "xclip -selection c"
# set -g @copytk-copy-command "tmux load-buffer -; tmux save-buffer - | xclip -selection c"
# set -g @copytk-no-default-matches off




set -wg window-status-format "#I:#W " # 状态栏窗口名称格式
set -wg window-status-current-format "#I:#W#F " # 状态栏当前窗口名称格式(#I：序号，#w：窗口名称，#F：间隔符)
set -wg window-status-separator "" # 状态栏窗口名称之间的间隔

set -g @cpu_low_icon "🔥" # icon when cpu is low
set -g @cpu_medium_icon "🔥" # icon when cpu is medium
set -g @cpu_high_icon "🔥" # icon when cpu is high
set -g @cpu_percentage_format "%2.0f%%"
set -g @cpu_low_bg_color "#[bg=green]" # background color when cpu is low
set -g @cpu_medium_bg_color "#[bg=orange]" # background color when cpu is medium
set -g @cpu_high_bg_color "#[bg=red]" # background color when cpu is high

# run-shell ~/.config/tmux/plugins/tmux-cpu/cpu.tmux
# run-shell ~/.config/tmux/plugins/tmux-battery/battery.tmux

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
# run '~/.tmux/plugins/tpm/tpm'
run '~/.config/tmux/plugins/tpm/tpm'
#🔼🔼🔼
